package com.ghck.server.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserCache;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ghck.server.dao.AjkAccountMapper;
import com.ghck.server.dao.MakerMapper;
import com.ghck.server.dao.UsersMapper;
import com.ghck.server.exception.ErrorConstant;
import com.ghck.server.exception.GhckException;
import com.ghck.server.model.AjkAccount;
import com.ghck.server.model.Maker;
import com.ghck.server.model.UserRole;
import com.ghck.server.model.Users;
import com.ghck.server.service.MakerService;
import com.ghck.server.utils.FileStoreUtil;

@Service
@Transactional
public class MakerServiceImpl implements MakerService{

	@Autowired
	private MakerMapper makerMapper;
	@Autowired
	private AjkAccountMapper ajkAccountMapper;
	@Autowired
	private UsersMapper usersMapper;
	@Autowired
	private UserCache userCache;
	
	@Override
	public Integer addMaker(Maker maker) {
		Maker dbMaker = makerMapper.findMakerByUserId(maker.getUserId());
		if(dbMaker != null){
			if(dbMaker.getStatus() == 0){
				GhckException exception = new GhckException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"您已经申请创客了,请等待审核");
				throw exception;
			}else if(dbMaker.getStatus() == 1){
				GhckException exception = new GhckException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"您已经是创客了");
				throw exception;
			}
		}
		Maker hs = makerMapper.findMakerByDistrictIdAndPass(maker.getDistrictId());
		if(hs != null){
			GhckException exception = new GhckException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"该区域已经有人申请通过了,请换个区域");
			throw exception;
		}
		Date date = new Date();
		maker.setCreateTime(date);
		maker.setLastModifyTime(date);
		maker.setStatus(0);
		return makerMapper.addMaker(maker);
	}

	@Override
	@Transactional(readOnly=true)
	public List<Maker> listAllMaker(Maker maker) {
		return null;
	}

	@Override
	public void makerPass(Maker maker) {
		Maker dbmaker = makerMapper.findMakerById(maker.getId());
		if(dbmaker == null){
			GhckException exception = new GhckException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"该创客申请不存在");
			throw exception;
		}else {
			if(1 == dbmaker.getStatus()){
				GhckException exception = new GhckException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"该商家已经审核过了");
				throw exception;
			}
			Maker hs = makerMapper.findMakerByDistrictIdAndPass(dbmaker.getDistrictId());
			if(hs != null){
				GhckException exception = new GhckException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"该区域已经有人申请通过了,请换个区域");
				throw exception;
			}
		}
		
		AjkAccount aa = ajkAccountMapper.findByUserId(dbmaker.getUserId());
		if(aa != null){
			userCache.removeUserFromCache(aa.getAccessId());
			makerMapper.updateMakerStatus(maker.getId(), 1);
			aa.setRole(UserRole.MAKER.ordinal());
			ajkAccountMapper.update(aa);
			Users user = new Users();
			user.setId(dbmaker.getUserId());
			user.setRole(UserRole.MAKER.ordinal());
			usersMapper.update(user);
		}
		
	}

	@Override
	public void makerReject(Maker maker) {
		makerMapper.updateMakerStatus(maker.getId(), 2);
	}

	@Override
	public List<Maker> listMakers(Maker maker, Integer start, Integer limit) {
		List<Maker> list = makerMapper.listMakers(maker, start, limit);
		if(CollectionUtils.isNotEmpty(list)){
			for(Maker item:list){
				if(StringUtils.isNotEmpty(item.getCardFront())){
					item.setCardFront(FileStoreUtil.getBaseUrl() + item.getCardFront());
				}
				if(StringUtils.isNotEmpty(item.getCardBack())){
					item.setCardBack(FileStoreUtil.getBaseUrl() + item.getCardBack());
				}
				if(StringUtils.isNotEmpty(item.getBusinessLicence())){
					item.setBusinessLicence(FileStoreUtil.getBaseUrl() + item.getBusinessLicence());
				}
			}
		}
		return list;
	}
	
	@Override
	public Integer countListMakers(Maker maker) {
		return makerMapper.countListMakers(maker);
	}
	

	@Override
	public Maker findMakerById(Integer id) {
		return makerMapper.findMakerById(id);
	}

	@Override
	public Maker findMakerByDistrictIdAndPass(Integer districtId) {
		return makerMapper.findMakerByDistrictIdAndPass(districtId);
	}

}